<?php
class WjbinviteAction extends WapAction{
    public function index(){
        $token = $_GET['token'];
        $id = $_GET['id'];
        if(!isset($id))exit;
        $model = M('Wjbinvite_data')->where(array('token'=>$token,'id'=>$id))->find();
        $arg = unserialize($model['arg']);
        $tplname = $model['tplname'];
        $tpl = M('Wjbinvite_model')->where(array('tplname'=>$tplname))->find();
        $config = unserialize($tpl['config']);

        $music = $model['music'];
        $backpic = $model['pic'];
        $title = $model['name'];

        //调用模板中的函数生成对应的html
        //加载模板的相关生成函数
        eval($tpl['tpl']);

        //生成好了放在变量中 还是直接到页面中去生成
        $content = array();
        foreach($arg as $key => $val ){
            $fun = $val['pagetype'];
            $content[$key] = $fun($val['pagearg'],array('id'=>$id,'token'=>$token));
        }

        //非屏幕操作的函数
        if(function_exists('commondom')){
            $commondom = commondom();
            $this->assign('commondom',$commondom);
        }

        $this->assign('js',$tpl['js']);
        $this->assign('css',$tpl['css']);
        //页面中只需要循环放在div中就可以了 good
        $this->assign('content',$content);

        if($music){
            $this->assign('music',$music);
        }
        if($backpic){
            $this->assign('backpic',$backpic);
        }

        $this->assign( 'title',$title );
        //var_dump($content);
        $this->display();
        //$this->display();
    }

    //手机场景邀约报名界面
    public function apply(){
        $token = $_GET['token'];
        $yid = $_GET['id'];//id是模板的id
        if(isset($_GET['do']) && $_GET['do'] == 'submit'){
            $data = array();
            $data['name'] = $_POST['name'];
            $data['mobile'] = $_POST['mobile'];
            $data['company'] = $_POST['company'];
            $data['token'] = $token;
            $data['yid'] = $yid;
            $data['intime'] = time();

            if(!$data['name'] || !$data['mobile'] || !$data['company']){
                $this->error("请不要留空");
            }

            $res = M('wjbinvite_user')->where(array('mobile'=>$data['mobile'],'yid'=>$yid))->find();
            if($res){
                $this->error("你已经报名了");
            }
            M('wjbinvite_user')->add($data);
            $this->success('报名成功','/index.php?g=Wap&m=Wjbinvite&a=lists&token='.$token.'&id='.$yid);
        }else{
            $this->assign('token',$token);
            $this->assign('id',$yid);
            $this->display();
        }

    }

    /**
     * 已经报名的用户列表
     */
    public function lists(){
        $token = $_GET['token'];
        $yid = $_GET['id'];
        $users = M('wjbinvite_user')->where(array('token'=>$token,'yid'=>$yid))->select();
        $this->assign('token',$token);
        $this->assign('users',$users);
        $this->assign('wecha_id',$this->wecha_id);
        $this->display();

    }
}